package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequestMapping("/member")
@RestController
public class MemberLoginController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 手机验证码效验
     * @param map
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody Map<String, String> map) {
        String phone = map.get("phone");
        String code = map.get("validateCode");
        //查询手机号是否注册
//        Boolean flag = memberLoginService.findPhone(phone);
//        if (!flag) {
//            System.out.println("请注册账户");
//        }
        //判断验证码是否正确
        if (!redisTemplate.opsForValue().get("CHECK_CODE_" + phone).equals(code)) {
            System.out.println(phone);
            System.out.println(redisTemplate.opsForValue().get("CHECK_CODE_" + phone));
            System.out.println(code);
            throw new RuntimeException("验证码错误");
        }
        redisTemplate.delete("CHECK_CODE_" + phone);

        return new Result(true,MessageConstant.LOGIN_SUCCESS,null);
    }
}
